// src/utils/dateUtils.js
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
import 'dayjs/locale/zh-cn' // 如果需要中文显示

// 配置插件和语言
dayjs.extend(relativeTime)
dayjs.locale('zh-cn') // 设置为中文环境

export const smartTimeDisplay = (isoString) => {
  const now = dayjs()
  const target = dayjs(isoString)
  const diffMinutes = now.diff(target, 'minute')

  if (diffMinutes < 1) return '刚刚'
  if (diffMinutes < 60) return `${diffMinutes}分钟前`
  if (diffMinutes < 1440) return `${Math.floor(diffMinutes/60)}小时前`
  if (diffMinutes < 10080) return `${Math.floor(diffMinutes/1440)}天前`
  return target.format('YYYY-MM-DD')
}

// 可选：直接使用 dayjs 的相对时间功能
export const formatRelativeTime = (isoString) => {
  return dayjs(isoString).fromNow()
}